import md5 from '../lib/md5str.js'
import Api from '../lib/api/api.js'

function formatNumber (n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}

export function formatTime (date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('/')
  const t2 = [hour, minute, second].map(formatNumber).join(':')

  return `${t1} ${t2}`
}

function _http(url,data,cb){
  data.token = '0ed3bcda-5989-4c0a-b4a9-ca96ac82d7dd';
  data.sunnyfix_timestamp = new Date().getTime();
  data.sunnyfix_sign = md5(url,data);
  let _this = this;
  wx.request({
    url:url,
    method:"POST",
    header: {
      'content-type': 'application/x-www-form-urlencoded' // 默认值
    },
    data:data,
    success: function(res){
      cb.call(_this,res)
    }
  })
}


export default {
  formatNumber,
  formatTime,
  _http,
  Api
}
